window.onload=function (){
    findAll(1)
}

function findAll(pageNo){
    axios.get("/room/findAll",{
        params:{
            pageNo:pageNo,
            info:$("roomNum1").value,
            room:$("address1").value,
            type:$("roomType").value,
            state:$("roomState").value
        }
    }).then(resp=>{
       var info= resp.data
        console.log(info)
        if (info === "noland"){
            location.href="/index.html?errorInfo=noland";
        }
        var str=""
        info.list.forEach(n=>{
            str+=`<tr><td>${n.address}</td><td>${n.number}</td><td>${n.resident}</td><td>${n.state}</td>
<td>${n.type}</td><td><input type="button" value="删除" onclick="del(${n.id})"  class="button12"><input type="button" value="查看房间" onclick="find(${n.id})"  class="button12"><input type="button" value="查看设施" onclick="findFacility(${n.id})"  class="button12"/></td>
</tr>`
        })
        $("data").innerHTML=str
        var pageStr = "";
        for (var i = 1; i <= info.totalPage; i++) {
            pageStr += `<a href="javascript:findAll(${i})" style="margin-left: 10px">${i} </a>`
        }
        $("cutDiv").innerHTML = pageStr;
    })
}

function  del(id){
    console.log(id)
        axios.get("/room/delRoom",{
            params:{
                roomId:id
            }
        }).then(resp=>{
          var info=  resp.data
            if (info==true){
                findAll(1)
            }
        })
}


function add(){
    $("addRoom").style.visibility="visible";
}


function addRomm(){
   axios.get("/room/addRoom",{
       params: {
           address:$("address").value,
           type:$("roomType2").value,
             number:$("manNum").value
       }
   }).then(rsep=>{
     var info=rsep.data
       if (info=="ok"){
           $("addRoom").style.visibility="hidden";
           findAll(1)
       }
   })
}

function find(id){
  $("find").style.visibility="visible"

    axios.get("/room/findByRooId",{
        params:{
            id:id
        }
  }).then(rsep=>{
    var info =rsep.data
        console.log(info)
          $("addressSpan").innerHTML=info.address
          $("NumSpan").innerHTML=info.resident
          $("typeSpan").innerHTML=info.type
          $("number").innerHTML=info.number
          $("stateSpan").innerHTML=info.state
        str2 =""
        info.studentBeans.forEach(n=>{
            console.log(n)
            str2+=`<tr><td>${n.name}</td><td>${n.classBean.nameClass}</td><td>${n.phone}</td></tr>`
        })
        $("data1").innerHTML=str2
  })

}
function affirm(){
    $("find").style.visibility="hidden"
    findAll(1)
}

function findFacility(id){
    $("roomId").value=id
    $("findf").style.visibility="visible";
    axios.get("fa/findByRoomId",{
        params:{
            roomId:id
        }
    }).then(resp=>{
        var info=resp.data
        console.log(info)
        let str3=""
        info.forEach(n=>{
            str3+=`<tr><td>${n.describe}</td><td>${n.date}</td><td>${n.state}</td>`
            if (n.state == "未解决"){
                str3 += `<td><input type="button" value="处理设施"onclick="update(${n.id})"  class="button12"/></td>`;
            }
            str3 += "</tr>";
        })

        $("data3").innerHTML=str3
    })
}
function quit(){
    $("findf").style.visibility="hidden"
    findAll(1)
}
function addf(){
    let id = $("roomId").value

    console.log(id)
    axios.get("fa/addFacility",{
            params:{
                roomId:id,
                describe:$("describe").value
            }
    }).then(resp=>{
     var info=resp.data
        if (info=="ok"){
            findFacility(id)
        }
    })
}
function update(id){
    console.log(id)
    let roodId = $("roomId").value
    console.log(roodId)
    axios.get("/fa/update",{
        params:{
            facilityId:id,
            roomId:roodId
        }
    }).then(resp=>{
       var info=resp.data
        if (info=="ok"){
            findFacility(roodId)
        }
    })
}